<?php

class Home extends Controller {

    function Home() 
    {
        parent::Controller();
        $this->load->library('cart');
    }

    function index() {
        $data = array();
        $data['judul'] = "Home";
        $data['judul_konten'] = "Selamat Datang";

		// 4 produk terbaru
        $data['produk_terbaru'] = $this->M_produk->get_produk_terbaru(4);
        $this->load->model('M_artikel');
        
        // 4 artikel terbaru
        $this->load->library('typography');
        $data['artikel_terbaru'] = $this->M_artikel->get_artikel_terbaru(4);
        
        $this->load->vars($data);
        $this->load->view('template_home');
    }
    
    
    function kritik_saran($halaman = 0)
    {

        $data = array();
        $data['judul'] = "Kritik dan Saran";
        $data['judul_konten'] = "Kritik dan Saran";
        $data['template_konten'] = "template_kritiksaran";

        $this->load->model('M_kritiksaran');
        $this->load->library('typography');
        $this->load->library('pagination');
        $this->load->library('form_validation');

        // pemroses posting

        if ($this->input->post('submit'))
        {
            // validasi dulu

            $this->form_validation->set_rules('input_nama', 'Nama', 'trim|required');
            $this->form_validation->set_rules('input_email', 'Email', 'trim|valid_email');
            $this->form_validation->set_rules('input_telp', 'Telepon', 'trim|required');
            $this->form_validation->set_rules('input_alamat', 'Alamat', 'trim|required');
            $this->form_validation->set_rules('input_isi_kritiksaran', 'Komentar', 'trim|required');

            if ($this->form_validation->run() == FALSE)
            {
                //redirect('home/kritik_saran#form');
            }
            else
            {
                // kalo valid
                if (!$this->session->userdata('id_user'))
                {
                    // inputs - unregistered user
                    $udata = array();
                    $udata['id_user'] = 0;
                    $udata['nama'] = $this->input->post('input_nama', TRUE);
                    $udata['telp'] = $this->input->post('input_telp', TRUE);
                    $udata['alamat'] = $this->input->post('input_alamat', TRUE);
                    $udata['email'] = $this->input->post('input_email', TRUE);
                    $udata['isi_kritiksaran'] = $this->input->post('input_isi_kritiksaran', TRUE);
                    $udata['tanggal_posting'] = date('Y-m-d H:i:s');
                    $udata['approved'] = 0;
                    $udata['ip'] = $_SERVER['REMOTE_ADDR'];
                }
                else
                {
                    // inputs - registered user
                    $udata = array();
                    $udata['id_user'] = $this->session->userdata('id_user');
                    $udata['nama'] = $this->input->post('input_nama', TRUE);
                    $udata['telp'] = $this->input->post('input_telp', TRUE);
                    $udata['alamat'] = $this->input->post('input_alamat', TRUE);
                    $udata['email'] = $this->input->post('input_email', TRUE);
                    $udata['isi_kritiksaran'] = $this->input->post('input_isi_kritiksaran', TRUE);
                    $udata['tanggal_posting'] = date('Y-m-d H:i:s');
                    $udata['approved'] = 1;
                    $udata['ip'] = $_SERVER['REMOTE_ADDR'];
                }

                if ($this->M_kritiksaran->tambahkan($udata)) redirect('home/kritik_saran_ok');

           }

        }


        // tampilkan


        $mulai = $this->uri->segment(2, 0);
        $limit_per_halaman = 6;

        $data['daftar_kritiksaran'] = $this->M_kritiksaran->get_entri_publik($mulai, $limit_per_halaman);

        $paging['base_url']     = site_url('kritik_saran');
        $paging['total_rows']   = $this->M_kritiksaran->jumlah_data_hasil;
        $paging['per_page']     = $limit_per_halaman;
        $paging['uri_segment']  = 2;
        $paging['num_links']    = 100;

        $this->pagination->initialize($paging);
		$data['page_links'] = $this->pagination->create_links();

        $this->load->vars($data);
        $this->load->view('template');

    }

    function kritik_saran_ok()
    {
        $data['success'] = TRUE;
        $data['judul'] = "Kritik dan Saran Berhasil di Kirim";
        $data['template_konten'] = "template_kritiksaran_post";

        $this->load->vars($data);
        $this->load->view('template');

    }

}
?>
